<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="当前判定" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkType">
              <j-dict-select-tag type="list" v-model="checkType" dictCode="check_type" :disabled="true"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="检测项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkItem">
              <a-input v-model="model.checkItem" placeholder="请输入检测项"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upperLimit">
              <a-input v-model="model.upperLimit" placeholder="请输入上限"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="下限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lowerLimit">
              <a-input v-model="model.lowerLimit" placeholder="请输入下限"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MesTechnologyProcedureCheckBomForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      procedureId: {
        type: String,
        require: true
      },
      checkType: {
        type: String,
        require: true
      }
    },
    data() {
      return {
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          checkItem: [
            { required: true, message: '请输入检测项!' }
          ]
        },
        url: {
          add: '/mes/mesTechnologyProcedureCheckBom/add',
          edit: '/mes/mesTechnologyProcedureCheckBom/edit',
          queryById: '/mes/mesTechnologyProcedureCheckBom/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created() {
      //设置工序外键
      this.model.procedureId=this.procedureId;

      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))

      //如果判定规则为自动判定，则必须输入上限和下限
      if (this.checkType==='1'){
        this.validatorRules.upperLimit=[{ required: true, message: '请输入上限!' }];
        this.validatorRules.lowerLimit=[{ required: true, message: '请输入下限!' }];
      }
    },
    methods: {
      add() {
        this.edit(this.modelDefault)
      },
      edit(record) {
        this.model = Object.assign({}, record)
        this.visible = true
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      }
    }
  }
</script>